// 获取临时存储的编辑数据
var data = JSON.parse(sessionStorage.getItem('editParams'));
// localstorage里面的模拟数据
var list = JSON.parse(localStorage.getItem('data') || '[]');
// form表单
var form = document.querySelector('#entryForm');
var aInp = document.querySelectorAll('#entryForm input');
// 取消按钮
var cancelBtn = document.querySelector('.cancelBtn');
// 点击取消按钮返回列表页面
cancelBtn.onclick = function pageJump() {
	event.preventDefault();
	data = {};
	var url = location.href;
	url = url.substring(0,url.lastIndexOf("/"));
	window.location.href = `${url}/Employee-Files-Education_Experience.html`
}
//console.log(sessionStorage.getItem('editParams'))
// 判断是编辑还是增加页面
if(sessionStorage.getItem('editParams')){//存在就是编辑页面
	for(var i = 0;i < form.elements.length; i++) {
		form.elements[i].value = data[form.elements[i].name] || '';
	}
}

// id相同，每一条数据对应一个编辑页内容
function editEmployees(param) {
	event.preventDefault();
	var id = JSON.parse(sessionStorage.getItem('editParams')).id;
	list.forEach(function (item,index) {
		if(item.id === id) {
			list[index] = param;
		}
	})
}


// 表单验证

var rule = {
	name: {
		reg: /^[\u4e00-\u9fa5]{2,6}$/,
		err: '请输入中文姓名'

	},
	number: {
		reg: /^\w{6,14}$/,
		err: '6-14位数字'
	},
	department: {
		reg: /^[\u4e00-\u9fa5]{2,6}$/,
		err: '产品部、IT部、信息部...'
	},
	position: {
		reg: /^[\u4e00-\u9fa5]{2,6}$/,
		err: '前端、产品经理'
	},
	educationBJ: {
		reg: /^[\u4e00-\u9fa5]{2}$/,
		err: '专科、本科'
	},
	educationSchool: {
		reg: /^[\u4e00-\u9fa5]{2,18}$/,
		err: '请输入正确的毕业院校'
	},
	major: {
		reg:/^[\u4e00-\u9fa5]{2,18}$/,
		err: '请输入正确的专业'
	},
	educationType: {
		reg:/^[\u4e00-\u9fa5]{2,6}$/,
		err: '统招、非统招、专升本'
	},
	startTime: {
		reg:/^\d{4}-\d{2}-\d{2}$/,
		err: 'yyyy-MM-dd'
	},
	endTime: {
		reg:/^\d{4}-\d{2}-\d{2}$/,
		err: 'yyyy-MM-dd'
	}
}
function test(obj){
	var str = obj.value;
	if (str.trim() === '') {
		obj.classList.add('red');
		return false;
	} else if (rule[obj.name].reg.test(str) === false) {
		obj.classList.add('red');
		obj.placeholder = rule[obj.name].err;
		return false;
	} else {
		obj.classList.remove('red');
		return true;
	}
}

//输入框
for (var i = 0; i < aInp.length-5; i++) {
	aInp[i].onblur = function () {
		test(this);
	}
}


// 修改、新增
function addEmployees() {
	event.preventDefault();
	var form = document.getElementById('entryForm');
	var formData = new FormData(form);// 实例化一个构造函数
	if(!(formData.get("name") && formData.get("number") && formData.get("department") && formData.get("position") && formData.get("educationBJ") && formData.get("educationSchool") && formData.get("major") && formData.get("startTime") && formData.get("endTime") && formData.get("educationType") && formData.get("isDegree"))) {
		alert('请完善信息');
		return;
	}
	for(var i = 0; i < aInp.length-5; i++) {
		if (test(aInp[i])===false) {
			alert('输入格式不正确，请修改');
			return;
		}
	}
	var param = {
		id: Date.now(),
		name:formData.get("name"),
		number:formData.get("number"),
		department:formData.get("department"),
		position:formData.get("position"),
		educationBJ:formData.get("educationBJ"),
		educationSchool:formData.get("educationSchool"),
		major:formData.get("major"),
		startTime:formData.get("startTime"),
		endTime:formData.get("endTime"),
		educationType:formData.get("educationType"),
		isDegree:formData.get("isDegree"),
	}
	if(sessionStorage.getItem('editParams')) {
		editEmployees(param);
	}else {// 反之是空的，说明是新增的，就用新增的方法
		list.unshift(param);
	}
	localStorage.setItem('data',JSON.stringify(list));
	var url = location.href;
	url = url.substring(0,url.lastIndexOf("/"));
	window.location.href = `${url}/Employee-Files-Education_Experience.html`
}


